/**
* 作者: lby
* 日期: 2024-11-06 20:23:17
* 备注: 用户教室槽位表
*/
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.commondata.child.ChildQuality;
import com.motu.monstercity.server.game.commondata.system.UserExp;
import com.motu.monstercity.server.game.logic.ChildManager;
import com.motu.monstercity.server.game.logic.GameUser;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import com.motu.vertx.module.utility.toolset.Tool;

public class UserChildCls extends UserChildClsModel {
	public UserChildCls() {
		super();
	}

	public UserChildCls(long userId, int clsId) {
		super();
		this.putUserId(userId);
		this.putClsId(clsId);
	}

	public void reset() {
		recoverEnergy();
	}

	// 扣除体力
	public void subEnergy(UserInfo userInfo, int value) {
		int balance = getEnergy();
		if (value > balance) {
			value = balance;
		}
		int max = ChildManager.takeChildRoomEnergyMax(userInfo);// 体力上限
		if (balance >= max && balance - value < max) {// // 原来的体力超过上限,且使用后低于下限,开始cd倒计
			putEnergyRecoverTime(TimeUtils.getCurTime());
		}
		super.addEnergy(-value);
	}

	// 恢复体力
	public void recoverEnergy() {
		UserInfo userInfo = GameUser.getUserInfo(getUserId());
		int max = ChildManager.takeChildRoomEnergyMax(userInfo);// 自动恢复的体力上限
		if (getEnergy() >= max) {// 此时体力已经达到上限
			return;
		}
		int cdTime = AllParam.CHILD_VIGOR_CD;// 体力恢复的CD时间
		long diff = TimeUtils.getTimeDiff(getEnergyRecoverTime());
		if (max == 0 || cdTime == 0 || diff < cdTime) {
			return;// 不需要执行恢复逻辑
		}

		int addNum = (int) Math.floor((double) diff / cdTime);  // 恢复体力点数
		if (getEnergy() + addNum < max) {
			putEnergyRecoverTime(getEnergyRecoverTime() + cdTime * addNum);// // 恢复了n点, 标记时间就加N个cdtime
		} else {
			addNum = max - getEnergy();
			putEnergyRecoverTime(TimeUtils.getCurTime());
		}
		addEnergy(addNum);
		update();
	}

	public void cleanChild(){
		this.putStudySchedule(0);
		this.putCareer(0);
		this.putCareerAddition(0);
		this.putPartner(0);
		this.putPartnerAddition(0);
		this.putSecretary(0);
		this.putStudySchedule(0);
		this.putQuality((byte)0);
		this.putAppearance((byte)0);
		this.putCharacterId((byte) 0);
		this.putIsTwins((byte)0);
		this.putSisterName("");
		this.putSex((byte) 0);
		this.putSisterSex((byte) 0);
		this.putName("");
		this.putIsStudy((byte) 0);
		this.putAddition(0);
	}

	// 是否可以学习
	public boolean isCanStudy() {
		ChildQuality childQuality = ChildManager.getChildQuality(getQuality());
		if (childQuality == null) {
			return false;
		}
		if (getIsStudy() > 0 && getEnergy() > 0 && !Tool.isEmpty(getName()) && getStudySchedule() < AllParam.CHILD_COURSE_MAX) {
			return true;
		} else {
			return false;
		}
	}

	// 每次上课,增加的进度
	public int takeAddStudy() {
		ChildQuality childQuality = ChildManager.getChildQuality(getQuality());
		return childQuality.getTrainingGrowth();
	}

	// 学习一次需要消耗的金币数量
	public long takeStudyConsume() {
		ChildQuality childQuality = ChildManager.getChildQuality(getQuality());
		if (childQuality == null) {
			return 0L;
		}
		return getBaseConsume() * childQuality.getPitNum() / 10000;
	}

	// 一键学习时,最大可以学习几次
	public int countMaxStudyNum(long userItemNum)  {
		int energy = getEnergy();// 体力
		int num =  (int) Math.ceil ((AllParam.CHILD_COURSE_MAX - getStudySchedule()) / (double)takeAddStudy());// 需要上课几次,进度才会满
		int min = Math.min(energy, num);

		long aa = takeStudyConsume();// 单次消耗的金币数量
		if (aa == 0) {// D D+ 品质培训不需要消耗金币
			return min;
		} else {
			int num2 = (int)Math.floor (userItemNum / (double)aa);
			return Math.min(min, num2);
		}
	}

	// 品质加成系数 天赋
	public int takeQualityEarnAdd() {
		ChildQuality childQuality = ChildManager.getChildQuality(getQuality());
		if (childQuality == null) {
			return 0;
		}
		return childQuality.getEarnSpeedAddition();
	}

	// 添加上课进度,返回增加的城市收益
	public long addStudySchedule(UserInfo userInfo, UserExp userExp, int addValue, boolean isTwins) {
		long addEarn = 0L;
		int totalNode = AllParam.CHILD_COURSE_MAX / AllParam.CHILD_COURSE_STAGE;// 总的节点数量
		int oldNode = ChildManager.takeNodeNum(getStudySchedule());// 原来的节点
		int newNode = ChildManager.takeNodeNum(getStudySchedule() + addValue);// 新的节点
		if (newNode > oldNode) {// 有跨节点
			int diffNode = newNode - oldNode;// 总共跨了几个几点
			long totalEarn =  ChildManager.countEarnAddBaseValue(userInfo, this, userExp);// // 子嗣 城市收益加成的基础值
			addEarn = totalEarn / totalNode * diffNode;// 本地提升上课进度,增加的城市收益
			if (isTwins) {
				addEarn = addEarn * 2;// 双胞胎
			}
			this.addAddition(addEarn);
		}
		super.addStudySchedule(addValue);
		return addEarn;
	}

	// 教室里子嗣的数量
	public int countChildNum() {
		if (checkIsTwins()) {
			return 2;
		} else {
			return 1;
		}
	}

	// 是否双胞胎
	public boolean checkIsTwins() {
		return getIsTwins() == 1;
	}

	// 是否有培养的红点
	public boolean checkRedPointStudy() {
		if (getIsStudy() == 0) {// 空教室
			return true;
		} else if (Tool.isEmpty(getName())) {// 子嗣未取名
			return true;
		} else {
			int addValue = getEnergy() * takeAddStudy();// 增加的教学进度
			if (getStudySchedule() + getEnergy() * takeAddStudy() >= AllParam.CHILD_COURSE_MAX) {
				return true;
			} else {
				return false;
			}
		}
	}
}
